package com.example.utils;

import org.springframework.stereotype.Component;

@Component
public class Page {
    private int start=1;//开始数据的索引
    private int count=10;//每页数量
    private int total;//总数据量

    public Page() {
    }

    public Page(int start, int count, int total) {
        this.start = start;
        this.count = count;
        this.total = total;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    /**
     * 是否有上一页
     * @return
     */
    public boolean isHasPreviouse(){
        if(start == 0){
            return false;
        }else{
            return true;
        }
    }
    /**
     * 是否有下一页
     * @return
     */
    public boolean isHasNext(){
        if(start == getLast()){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 计算得到的总页数
     * @return
     */
    public int getTotalPage(){
        int totalPages=0;
        if(total%count==0){
            totalPages=total/count;
        }else{
            totalPages=total/count+1;
        }
        if(totalPages==0){//当不满一页时
            totalPages=1;
        }
        return totalPages;
    }

    /**
     * 获取最后一页
     * @return
     */
    public int getLast(){
        int last=0;
        if(total%count==0){
            last=total-count;
        }else{
            last=total-total%count;
        }
        last=last<0 ? 0:last;
        return last;
    }

    @Override
    public String toString() {
        return "Page{" +
                "start=" + start +
                ", count=" + count +
                ", total=" + total +
                '}';
    }
}
